package com.ssm.controller;


import com.ssm.bean.District;
import com.ssm.service.DistrictService;
import com.utils.Meg;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class DistrictController {
    @Autowired
    private DistrictService districtService;

    @RequestMapping("/districtView")
    public String districtView(){
        return "district/district";
    }

    @RequestMapping("/districtEditView")
    public String districtEditView(){
        return "district/districtPage";
    }

    @RequestMapping("/districtAddView")
    public String districtAddView(){
        return "district/districtAdd";
    }

    @ResponseBody
    @RequestMapping("/districtData")
    public Meg districtData(@Param("page") String page, @Param("limit") String limit){
        return Meg.success().add("data",districtService.selectAll(limit,page));
    }

    @ResponseBody
    @RequestMapping("/districtInsert")
    public Meg districtInsert(District district){
        int i = districtService.insert(district);
        if (i>0)
            return Meg.success();
        else
            return  Meg.file();
    }

    @ResponseBody
    @RequestMapping("/districtUpdate")
    public Meg districtUpdate(District district){
        System.out.println(district.getName());
        int i = districtService.update(district);
        if (i>0)
            return Meg.success();
        else
            return  Meg.file();
    }

    @ResponseBody
    @RequestMapping("/districtDelete")
    public Meg districtDelete(@Param("id") Integer id){
        int i = districtService.delete(id);
        if (i>0)
            return Meg.success();
        else
            return  Meg.file();
    }
    @ResponseBody
    @RequestMapping("/districtList")
    public Meg getAll(){
        return Meg.success().add("data",districtService.selectAll());
    }
}
